package com.ftg.learn.framework.mybatis.javabase.util;

import com.mysql.cj.jdbc.MysqlDataSource;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.Transaction;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransaction;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.apache.ibatis.transaction.managed.ManagedTransaction;
import org.apache.ibatis.session.Configuration;
import javax.sql.DataSource;
import java.io.InputStream;

/**
 *
 * 通用的功能设计封装
 *
 * 对mybatis通用部分进行面向对象的封装
 *
 * @author lijiaxin
 * @version 1.0
 * @date 2023/9/4 10:05
 */

public class MybatisUtil {

    private final String url="jdbc:mysql://localhost:3306/j2303?timeZone=Asia/Harbin&characterEncoding=utf-8";
    private final String user="root";
    private final String password="lijiaxin52";

    /**
     * 想使用mybatis 一定要有四大基础信息
     * 1 驱动类
     * 2 mybatis 包
     * 3 : mysql 四大基础信息
     * 3.1 乱码  时区的问题
     * 4 : 想办法  3--> sqlSessionFactoryBuilder -->  sqlSessionFactory -->   sqlSession  --> getMapper(） --> 接口代理 ->> 调用里面的方法
     */

    /**
     * 用来生成数据源
     *
     * @param url      连接串
     *                 ex jdbc://localhost:3306/j2303?timeZone=Asia/Harbin&characterEncoding=utf-8
     * @param user     用户名
     * @param password 密码
     * @return
     */

    private DataSource createDtaSourse(String url,String user,String password){
        MysqlDataSource dataSource=new MysqlDataSource();
            dataSource.setUser(user);
            dataSource.setURL(url);
            dataSource.setPassword(password);
            return  dataSource;
    }

    private DataSource createDtaSourse(){
        MysqlDataSource dataSource=new MysqlDataSource();
        dataSource.setUser(this.user);
        dataSource.setURL(this.url);
        dataSource.setPassword(this.password);
        return  dataSource;
    }

    /**
     * 为我们产生了一个环境类吧
     * @return
     */

    private Environment initEnv(){
        TransactionFactory transactionFactory=new JdbcTransactionFactory();
        Environment e=new Environment("ljxlocalmysql", transactionFactory, this.createDtaSourse());
        return e;
    }

    private Configuration initConfiguration(){
        Configuration configuration = new Configuration();
            //setting
        configuration.setCacheEnabled(true);
        //打开sql打印功能，开发阶段
        configuration.setLogImpl(StdOutImpl.class);
        //把环境给配置
        configuration.setEnvironment(this.initEnv());
        //configuration添加映射器
        configuration.addMappers("com.ftg.learn.framework.mybatis.javabase.mapper");


        //没有mybatis缓存当中找到  showListWithParameter 的映射 mapper 就叫映射 sql
        //sql 接口里注解 还有一个就是xml 。就是没有直接找到把一个.xml注入mapper缓存的方法

        // 读取 Student.xml 映射文件
        String resource = "com/ftg/learn/framework/mybatis/javabase/mapper/xml/student.xml"; // 替换为你的映射文件路径
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream(resource);

        // 使用 XMLMapperBuilder 注入映射文件
        XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(inputStream, configuration, resource, null);
        xmlMapperBuilder.parse();

        return configuration;
    }

    public SqlSession getSqlSession(){
        //工厂建造的实例化
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder();
        //实例化工厂
        SqlSessionFactory sqlSessionFactory=sqlSessionFactoryBuilder.build(this.initConfiguration());
        return sqlSessionFactory.openSession();
    }

    public void commit(SqlSession session){
        session.commit();
    }
    public void rollback(SqlSession session){
        session.rollback();
    }
    public void close(SqlSession session){
        session.close();
    }

}
